<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>添加/修改账号</title>
    <%@ include file="/view/common/tags.jsp" %>
    <style type="text/css">
        .accountNum {
            width: 45px;
            margin-right: 20px;
            height: 25px;
        }

        .divl {
            width: 50%;
            float: left;
        }

        .divr {
            width: 50%;
            float: left;
            padding-left: 10px;
        }
    </style>
</head>
<body>
<div class="well well_p">
    <input type="hidden" id="pageFlag" value="<%=request.getParameter("flag")%>">
    <sk:form method="post" cssClass="form-horizontal" id="saveAndUpdateFrom" target="_parent" action="">
        <div class="form-group">
            <div id="allotDiv" style="display: none;">
                <div class="form-group">
                    <label class="col-xs-2 text-right">平台:</label>
                    <div class="col-xs-7">
                        <sk:select cssClass="form-control select" name="platformType1" id="platformType1" value=""
                                   requiredInit="true" ajaxURL="${sysManagerV1}/baseType/select?wm[PARENT_ID]=20"
                                   onchange="getAllNum(this.value)">
                            <option value="">请选择</option>
                        </sk:select>
                        <input type="hidden" name="platformName" id="platformName" value="">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-2 text-right">项目:</label>
                    <div class="col-xs-7">
                        <input type="text" class="form-control" name="projectName" id="projectName" value=""
                               placeholder="输入项目">
                    </div>
                </div>
            </div>
            <div id="reAllotDiv" style="display: none;">
                <div class="form-group">
                    <label class="col-xs-2 text-right">选择项目:</label>
                    <div class="col-xs-7">
                        <sk:select cssClass="form-control select" name="platformType2" id="platformType2" value=""
                                   requiredInit="true" ajaxURL="${sysManagerV1}/cardBatch/list/new"
                                   onchange="getNotAllotNum(this.value)">
                            <option value="">请选择</option>
                        </sk:select>
                        <input type="hidden" name="platformName" id="platformName" value="">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-xs-2 text-right">&nbsp;</label>
                <div class="col-xs-9">当前平台可分配帐号数量：<span id="notAllotNum">0</span></div>
            </div>
            <div class="form-group">
                <label class="col-xs-2 text-right">分配人:</label>
                <input type="hidden" id="kfNum" value="">
                <div class="col-xs-10" id="kfDiv"></div>
                <input type="hidden" id="kfInfo" name="data" value="">
            </div>

            <input type="hidden" id="rowId" name="rowId" value="<%=request.getParameter("rowId")%>">
            <input type="hidden" id="platformType" name="platformType" value="">
            <input type="hidden" id="token" name="token" value="">
            <input type="hidden" id="flag" value="${flag}">
            <input type="hidden" id="isReCommit" value="0">
        </div>
    </sk:form>
</div>
</body>
<%@ include file="/view/common/footer.jsp" %>
<script type="text/javascript">
    $(function () {
        $("#token").val(sessionStorage.getItem("token"));
        var rowId = $("#rowId").val();

        var pageFlag = $("#pageFlag").val();
        if (pageFlag == "reAllot") {
            $("#reAllotDiv").show();
        } else {
            $("#allotDiv").show();
        }

        $.ajax({
            url: "${sysManagerV1}/staff/getCardKfList",
            type: "post",
            cache: false,
            data: {"rowId": rowId},
            dataType: "json",
            async: false,
            success: function (data) {
                if (data == null) {
                    return;
                }
                data = eval(data);
                var html = "";
                for (var i in data) {
                    html += "<div class='" + (i % 2 == 0 ? "divr" : "divl") + "'>"
                    html += "<label for='port'><input type='checkbox' id='checkBox" + i + "' name='kfList[" + i + "].kfId' kfId='" + data[i].rowId + "' value='" + i + "'>";
                    html += "<span>" + data[i].staffAccount + "</span>&nbsp;&nbsp;";
                    html += "<input type='text' id='accountNum" + data[i].rowId + "' name='kfList[" + i + "].accountNum' value='' class='accountNum'></label>";
                    html += "</div>";
                }
                $("#kfDiv").html(html);
                $("#kfNum").val(data.length);
            }
        });
    })

    var err = {'1': '分配', '2': '重新分配'};
    var action = {'1': '/allocation', '2': '/residue/allocation'};

    function allot() {
        var i;
        var pageFlag = $("#pageFlag").val();
        if (pageFlag == "reAllot") {
            i = 2;//重新分配
        } else {
            i = 1;//分配
        }

        var platformType = $.trim($("input[name='platformType']").val());
        if (!CheckUtil.isNotNull(platformType)) {
            layer.msg("请选择平台!");
            $("#platformType").focus();
            return;
        }
        $("#platformName").val($("#platformType option:selected").text());

        var projectName = $.trim($("#projectName").val());
        if (i == 1 && !CheckUtil.isNotNull(projectName)) {
            layer.msg("请填写项目名称!");
            $("#projectName").focus();
            return;
        }

        // 验证、并组装客服信息
        var kfInfo = {};
        var kfId, accountNum;
        var kfNum = $("#kfNum").val();
        $("input[type='checkbox']").each(function (i) {
            kfId = $(this).attr("kfId");
            accountNum = $("#accountNum" + kfId).val();

            if ($(this).is(':checked') && !CheckUtil.isInt(accountNum)) {
                layer.msg("请填正确的帐号数量!");
                $("#accountNum" + kfId).focus();
                return;
            }

            if ($(this).is(':checked') && CheckUtil.isInt(accountNum)) {
                kfInfo[kfId] = accountNum;
            }
        });
        $("#kfInfo").val(JSON.stringify(kfInfo));

        if (kfInfo.length == 0) {
            layer.msg("请选择分配的客服!");
            return;
        }

        //loading层
        var index = layer.load(0, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });

        $.ajax({
            url: "${sysManagerV1}/cardUser" + action[i],
            type: "post",
            cache: false,
            data: new FormData($("#saveAndUpdateFrom")[0]),
            dataType: "json",
            processData: false,
            contentType: false,
            async: false,
            success: function (data) {
                if (data.code == 0) {
                    parent.layer.msg("分配成功！");
                } else if (data.code == "-2") {
                    parent.window.location.href = "${sysManagerV1}/";
                } else {
                    parent.layer.msg(data.message);
                }
                if (data.code >= 0) {
                    parent.findData();
                    parent.layer.close(parent.tableIndex);
                }
            }
        });

    }

    // 获取未分配帐号的数量
    function getNotAllotNum(platformType) {
        $("#platformType").val(platformType);
        $.ajax({
            url: "${sysManagerV1}/cardUser/getNotAllotAccountNum",
            type: "post",
            cache: false,
            data: {"platformType": platformType, "token": $("#token").val()},
            dataType: "json",
            async: false,
            success: function (data) {
                if (data == null) {
                    return;
                }
                var html = "" + data.data;
                $("#notAllotNum").html(html);
            }
        });
    }

    // 获取未分配帐号的数量
    function getAllNum(platformType) {
        $("#platformType").val(platformType);
        $.ajax({
            url: "${sysManagerV1}/cardUser/getAccountNum",
            type: "post",
            cache: false,
            data: {"platformType": platformType, "token": $("#token").val()},
            dataType: "json",
            async: false,
            success: function (data) {
                if (data == null) {
                    return;
                }
                var html = "" + data.data;
                $("#notAllotNum").html(html);
            }
        });
    }
</script>
</html>